package com.rui.study.algorithm.L_算法与数据结构_队列_数组实现;

import com.google.common.util.concurrent.RateLimiter;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-03-08 08:34
 **/
public class Main {

    public static void main(String[] args) {
        Queue q = new Queue(8);

        for (int j = 0; j < 4; j++) {
            // 生产者
            Thread producer = new Thread(()->{
                RateLimiter rateLimiter = RateLimiter.create(10);
                int start = 0;
                for (int i = 0; i < 1000; i++) {
                    rateLimiter.acquire();
                    if (i % 10 == 0 && i > start) {
                        Queue.Worker worker = new Queue.SimpleWorker(start, i);
                        start = i;
                        q.enqueue(new Queue.Node(worker));
                    }
                }
            }, "生产者-thread-" + j);
            producer.start();
        }

        for (int j = 0; j < 8; j++) {
            // 消费者
            Thread consumer = new Thread(()->{
                RateLimiter rateLimiter = RateLimiter.create(0.3d);
                while (true) {
                    rateLimiter.acquire();
                    Queue.Node node = q.dequeue();
                    if (node != null) {
                        node.worker.work();
                    }
                }
            }, "消费者-thread-" + j);
            consumer.start();
        }
    }
}
